package com.cskaoyan.order.service.impl;

import com.cskaoyan.order.biz.TransOutboundInvoker;
import com.cskaoyan.order.biz.context.AbsTransHandlerContext;
//import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.biz.factory.OrderProcessPipelineFactory;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.CreateOrderRequest;
import com.cskaoyan.order.service.OrderCoreService;
import com.github.pagehelper.PageHelper;
import com.sun.org.apache.xml.internal.dtm.ref.DTMStringPool;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.statement.select.OrderByElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@Service
public class OrderCoreServiceImpl implements OrderCoreService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderProcessPipelineFactory orderProcessPipelineFactory;

    @Autowired
    StockMapper stockMapper;

    @Autowired
    OrderConverter orderConverter;


    /**
     * 创建订单的处理流程
     *
     * @param request
     * @return
     */
    @Override
    public String createOrder(CreateOrderRequest request) throws InterruptedException, ExecutionException, IOException {
        //创建pipeline对象
        TransOutboundInvoker invoker = orderProcessPipelineFactory.build(request);

        //启动pipeline
        invoker.start(); //启动流程（pipeline来处理）

        //获取处理结果
        AbsTransHandlerContext context = invoker.getContext();

        //从Context中获取订单Id
        String orderId = context.getOrderId();
        return orderId;
    }

    //取消订单
    @Override
    public void cancelOrder(String orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        order.setStatus(7);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    //删除订单
    @Override
    public void deleteOrder(String orderId) {
        Order order = new Order();
        order.setOrderId(orderId);
        orderMapper.delete(order);
    }


    @Override
    public void updateOrder(Integer status, String orderId) {
        Order order = new Order();
        order.setStatus(status);
        order.setOrderId(orderId);
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     * 展示所有订单
     * @param page
     * @param size
     * @param sort
     * @param uid
     * @return
     */
    @Override
    public OrderListResponse selectOrderList(Integer page, Integer size, String sort,Long uid) {

        OrderListResponse orderListResponse = new OrderListResponse();

        Example example2 = new Example(Order.class);
        example2.and().andEqualTo("userId",uid);
        List<Order> orders2 = orderMapper.selectByExample(example2);

        PageHelper.startPage(page,size); // 分页

        Example example = new Example(Order.class);
        example.and().andEqualTo("userId",uid);
        example.setOrderByClause("update_time desc");


        List<Order> orders = orderMapper.selectByExample(example);
        List<OrderDetailInfo> orderDetailInfos = new LinkedList<>();

        for (Order order : orders) {
            String orderId = order.getOrderId();

            Example example1 = new Example(OrderItem.class);

            example1.and().andEqualTo("orderId",orderId);
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);

            List<OrderItemDto> orderItemDtos=orderConverter.item2dto(orderItems);


            OrderShipping shipping = orderShippingMapper.selectByPrimaryKey(orderId);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(shipping);

            OrderDetailInfo info = orderConverter.order2detail(order);
            info.setOrderItemDto(orderItemDtos);
            info.setOrderShippingDto(orderShippingDto);

            orderDetailInfos.add(info);
        }
        orderListResponse.setData(orderDetailInfos);
        orderListResponse.setTotal((long) orders2.size());

        return orderListResponse;
    }

}
